package com.test.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.R;
import com.test.entity.News;

import java.util.List;

/**
 * 交错风格文章列表
 *
 * @Author Musique
 * 2020/8/4
 */
public class NewsAdapterTwo extends BaseAdapter {

    private List<News> listNew;
    private Context mContext;

    //定义两种列表类型
    private final static int TYPE_ONE = 0;
    private final static int TYPE_TWO = 1;


    public NewsAdapterTwo(List<News> listNew, Context mContext) {
        this.listNew = listNew;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return listNew.size();
    }

    @Override
    public Object getItem(int i) {
        return listNew.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int i) {
        if (i % 2 == 0) {
            //整除
            return TYPE_ONE;
        } else {
            return TYPE_TWO;
        }
    }

    // 返回2中类型
    public int getTypeAccount() {
        return 2;
    }

    /**
     * 资源优化
     */
    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        int type = getItemViewType(i);//获取布局
        ViewHolder holder = new ViewHolder();
        ViewHolderTwo holderTwo = new ViewHolderTwo();
        // 加载listview_demo.xml文件（每次运行都要加载，浪费资源，因此如下）
        if (convertView == null) {
            switch (type) {
                case TYPE_ONE:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_demo, parent, false);
                    holder.img = convertView.findViewById(R.id.img_icon);
                    holder.title = convertView.findViewById(R.id.tv_title);
                    holder.content = convertView.findViewById(R.id.tv_content);
                    convertView.setTag(holder);
                    break;
                case TYPE_TWO:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_demo_two, parent, false);
                    holderTwo.img = convertView.findViewById(R.id.img_icon);
                    holderTwo.title = convertView.findViewById(R.id.tv_title);
                    holderTwo.content = convertView.findViewById(R.id.tv_content);
                    convertView.setTag(holderTwo);
                    break;
            }
            switch (type) {
                case TYPE_ONE:
                    holder = (ViewHolder) convertView.getTag();
                    break;
                case TYPE_TWO:
                    holderTwo = (ViewHolderTwo) convertView.getTag();
                    break;
            }
            switch (type) {
                case TYPE_ONE:
                    holder.img.setBackgroundResource(listNew.get(i).getAIcon());
                    holder.title.setText(listNew.get(i).getTitle());
                    holder.content.setText(listNew.get(i).getContent());
                    break;
                case TYPE_TWO:
                    holderTwo.img.setBackgroundResource(listNew.get(i).getAIcon());
                    holderTwo.title.setText(listNew.get(i).getTitle());
                    holderTwo.content.setText(listNew.get(i).getContent());
                    break;
            }
        }
        return convertView;
    }

    /**
     * 创建一个存储对象静态对象方法
     */
    public static class ViewHolder {
        ImageView img;
        TextView title, content;
    }

    /**
     * 创建一个存储对象静态对象方法
     */
    public static class ViewHolderTwo {
        ImageView img;
        TextView title, content;
    }
}
